package com.jetbrains.licenseServer.openapi.util;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DnsUtil {
    public static final String DEFAULT_DNS_ATTRIBUTE_NAME_PREFIX = "_jetbrains-license-server";
    public static final String DNS_ATTRIBUTE_NAME_PREFIX_PROPERTY = "dns.attribute.name.prefix";
    private static final String[] a = {"TXT"};

    static String calcHostName() {
        try {
            return a();
        } catch (Throwable localThrowable) {
        }
        return "<unknown>";
    }

    public static String detectServerUrl() {
        try {
            return (String) c(b()).get("url");
        } catch (Exception localException) {
        }
        return "http://jetbrains-license-server";
    }

    private static void a(String paramString, Processor<String> paramProcessor)
            throws UnknownHostException {
        HashSet localHashSet = new HashSet();
        Object localObject1;
        Object localObject2;
        try {
            Enumeration localEnumeration = NetworkInterface.getNetworkInterfaces();
            while (localEnumeration.hasMoreElements()) {
                localObject1 = (NetworkInterface) localEnumeration.nextElement();
                localObject2 = ((NetworkInterface) localObject1).getInetAddresses();
                while (((Enumeration) localObject2).hasMoreElements()) {
                    InetAddress localInetAddress1 = (InetAddress) ((Enumeration) localObject2).nextElement();
                    if (!a(localInetAddress1, localHashSet, paramProcessor)) {
                        return;
                    }
                }
            }
        } catch (SocketException localSocketException) {
        }
        if (paramString != null) {
            String[] arrayOfString = paramString.split(" ");
            localObject1 = Pattern.compile("dns://(\\d+\\.\\d+\\.\\d+\\.\\d+)");
            for (CharSequence localCharSequence : arrayOfString) {
                Matcher localMatcher = ((Pattern) localObject1).matcher(localCharSequence);
                if (localMatcher.matches()) {
                    InetAddress localInetAddress2 = InetAddress.getByName(localMatcher.group(1));
                    if (!a(localInetAddress2, localHashSet, paramProcessor)) {
                        return;
                    }
                }
            }
        }
        a(InetAddress.getLocalHost(), localHashSet, paramProcessor);
    }

    private static boolean a(InetAddress paramInetAddress, Collection<String> paramCollection, Processor<String> paramProcessor) {
        if ((paramInetAddress.isMulticastAddress()) || ((paramInetAddress instanceof Inet6Address)) || (paramInetAddress.isLoopbackAddress())) {
            return true;
        }
        Object localObject = paramInetAddress.getCanonicalHostName();
        for (; ; ) {
            String str = b((String) localObject);
            if (!a(str)) {
                return true;
            }
            if (!paramCollection.add(str)) {
                return true;
            }
            if (!paramProcessor.process(str)) {
                return false;
            }
            localObject = str;
        }
    }

    private static boolean a(String paramString) {
        for (int i = 0; i < paramString.length(); i++) {
            if (Character.isLetter(paramString.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    private static String b(String paramString) {
        int i = paramString.indexOf('.');
        return i > 0 ? paramString.substring(i + 1) : "";
    }

    private static String a()
            throws UnknownHostException {
        return InetAddress.getLocalHost().getCanonicalHostName();
    }

    private static String b()
            throws NamingException, UnknownHostException {
        Hashtable localHashtable = new Hashtable();
        localHashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
        InitialDirContext localInitialDirContext = new InitialDirContext(localHashtable);
        final String str1 = System.getProperty("dns.attribute.name.prefix", "_jetbrains-license-server");
        String str2 = (String) localInitialDirContext.getEnvironment().get("java.naming.provider.url");

        final String[] arrayOfString = {null};
        Processor local1 = new Processor() {
            public boolean process(String paramAnonymousString) {
                try {
                    Attributes localAttributes = this.val$context.getAttributes(str1 + "." + paramAnonymousString, DnsUtil.a);
                    arrayOfString[0] = ((String) localAttributes.get("TXT").get());
                    return false;
                } catch (Exception localException) {
                }
                return true;
            }
        };
        a(str2, local1);

        String str3 = arrayOfString[0];
        if (str3 != null) {
            return str3;
        }
        throw new NamingException("No license server DNS entries detected in any scanned local domains");
    }

    private static Map<String, String> c(String paramString) {
        HashMap localHashMap = new HashMap();

        StringTokenizer localStringTokenizer = new StringTokenizer(paramString, ";", false);
        while (localStringTokenizer.hasMoreTokens()) {
            String str1 = localStringTokenizer.nextToken();
            int i = str1.indexOf('=');
            if (i > 0) {
                String str2 = str1.substring(0, i).trim();
                String str3 = str1.substring(i + 1).trim();
                localHashMap.put(str2, str3);
            }
        }
        return localHashMap;
    }

    static abstract interface Processor<T> {
        public abstract boolean process(T paramT);
    }
}



/* Location:           E:\idea\

 * Qualified Name:     com.jetbrains.licenseServer.openapi.util.DnsUtil

 * JD-Core Version:    0.7.0.1

 */